package com.core.dao;

import com.core.domain.UserParse;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;

import java.sql.Time;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Timer;

/**
 * @program: JSPStudy
 * @description:
 * @author: djs
 * @create: 2023-04-27 16:49
 **/

@Repository
public interface DjsDao {

    @Select("SELECT id FROM stadium WHERE  isDelete = 0")
    List<Integer> selectTeamId();

    @Select("SELECT stadiumName FROM stadium WHERE id = #{teamId}")
    String selectTeamNameByTeamId(@Param("teamId") Integer teamId);

    @Select("SELECT COUNT(userId) FROM `usestadium` WHERE teamId = #{teamId} and isDelete =0 AND reservationStartTime > #{startTime} AND reservationEndTime < #{endTime} and isDelete = 0")
    Integer selectCountOfTeam(@Param("teamId") Integer teamId, @Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("SELECT rentalPrice FROM stadium s WHERE s.id= #{teamId}")
    Integer selectMoneyOfTeam(Integer teamId);

    @Select("SELECT stadiumNum FROM stadium WHERE id = #{teamId} AND isDelete = 0")
    Integer selectEachTeamNumber(@Param("teamId") Integer teamId);


    @Select("SELECT stadiumName FROM stadium ")
    List<String> selectAllTeamNames();


    @Select("SELECT count(*) FROM `user` u JOIN usestadium us ON u.id = us.userId JOIN stadium s ON us.teamId = s.id WHERE u.gender = #{i} AND u.isDelete = 0 AND s.stadiumName = #{eachTeamName} AND reservationStartTime > #{startTime} AND reservationEndTime < #{endTime} and us.isDelete = 0")
    Integer selectTeamOfAllNumbers(@Param("eachTeamName") String eachTeamName, @Param("i") int i, @Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("SELECT count(*) FROM `user` u JOIN usestadium us ON u.id = us.userId JOIN stadium s ON us.teamId = s.id WHERE u.gender = #{i} AND u.isDelete = 0 AND s.stadiumName = #{eachTeamName} AND us.isFantastic=0 AND reservationStartTime > #{startTime} AND reservationEndTime < #{endTime} and us.isDelete = 0")
    Integer selectTeamOfPopularNumbers(@Param("eachTeamName") String eachTeamName, @Param("i") int i, @Param("startTime") String startTime, @Param("endTime") String endTime);


    @Select("SELECT count(*) FROM `user` u JOIN usestadium us ON us.userId = u.id JOIN stadium s ON s.id = us.teamId WHERE s.stadiumName = #{eachTeamName} AND u.age BETWEEN #{startAge} AND #{endAge} AND reservationStartTime > #{startTime} AND reservationEndTime < #{endTime} AND u.isDelete = 0 and us.isDelete = 0")
    Integer selectEachTeamEachAllAgeNumbers(@Param("eachTeamName") String eachTeamName, @Param("startAge") Integer startAge, @Param("endAge") Integer endAge, @Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("SELECT count(*) FROM `user` u JOIN usestadium us ON us.userId = u.id JOIN stadium s ON s.id = us.teamId WHERE s.stadiumName = #{eachTeamName} AND u.age BETWEEN #{startAge} AND #{endAge} AND us.isFantastic=#{i} AND reservationStartTime > #{startTime} AND reservationEndTime < #{endTime} AND u.isDelete = 0 and us.isDelete = 0")
    Integer selectEachTeamEachAgeNumbers(@Param("eachTeamName") String eachTeamName, @Param("startAge") Integer startAge, @Param("endAge") Integer endAge, @Param("i") int i, @Param("startTime") String startTime, @Param("endTime") String endTime);


    @Select("SELECT us.id,u.userAccount,stadiumName,us.reservationStartTime,us.reservationEndTime,us.isFantastic FROM `user` u JOIN usestadium us ON u.id = us.userId JOIN stadium s ON us.teamId = s.id WHERE u.id= #{userId} and us.reservationEndTime<#{nowTime} and us.isDelete = 0")
    @Results(value = {
            @Result(column = "reservationStartTime", property = "startTime"),
            @Result(column = "reservationEndTime", property = "endTime"),
            @Result(column = "isFantastic", property = "fantastic")
    })
    List<UserParse> selectUserParse(@Param("userId") Object userId, @Param("nowTime")LocalDateTime nowTime);

    @Update("UPDATE usestadium SET isFantastic = #{parse} WHERE id = #{orderId}")
    int updateUserParse(@Param("orderId") String orderId, @Param("parse") int parse);
}
